package com.info.zhiduoduo.repository.mapper.company;

import com.baomidou.mybatisplus.mapper.BaseMapper;
import com.info.zhiduoduo.repository.dto.company.GetComplianceListDto;
import com.info.zhiduoduo.repository.dto.company.IntelligentComplianceDisplayDto;
import com.info.zhiduoduo.repository.entity.company.ComplianceIntelligence;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 *智合规
 * @author
 * @since 2022/5/16
 */
public interface ComplianceIntelligenceMapper extends BaseMapper<ComplianceIntelligence> {

    @Select("SELECT c.compname,ci.total,c.compid,ci.publish, " +
            "CASE WHEN ci.total IS NULL THEN 1 ELSE 2 END AS whetherToScore " +
            "FROM zdd_company c " +
            "LEFT JOIN zdd_compliance_intelligence ci ON ci.compid = c.compid  " +
            "WHERE c.compname LIKE '%${compName}%'  " +
            "AND c.state = 4  " +
            "AND IF(ISNULL(#{publish}),1=1,ci.publish = #{publish})  " +
            "having IF(#{whetherToScore}=0,1=1,whetherToScore = #{whetherToScore})  " +
            "ORDER BY ci.total DESC ")
    List<GetComplianceListDto> getComplianceList(@Param("compName") String compName,
                                                   @Param("whetherToScore") int whetherToScore,
                                                   @Param("publish") Integer publish);
    @Select("SELECT (( SELECT count( 1 ) FROM zdd_compliance_intelligence )- ( SELECT count( 1 ) FROM zdd_compliance_intelligence zdi WHERE zdi.total > ci.total ) \n" +
            ") / ( SELECT count( 1 ) FROM zdd_compliance_intelligence ) * 100 AS rankingProportion \n" +
            "FROM " +
            "zdd_compliance_intelligence ci  " +
            "WHERE " +
            "ci.compid = #{compid}")
    String getRankingProportion(@Param("compid") Integer compid);

    @Select("SELECT " +
            "ci.compid, " +
            "ci.enterprise_certification_opinions, " +
            "(ci.business_license + ci.introduction_to_main_business + ci.risk_control_restriction + ci.listed_company + ci.employment_scale + ci.business_circles + ci.registered_capital ) AS enterpriseCertificationPoints, " +
            "ci.data_integrity_opinion, " +
            "( ci.front_desk_photos + ci.billing_information + ci.contact_information + ci.corporate_information + ci.company_website ) AS dataIntegrityPoints, " +
            "ci.business_rationality_opinion, " +
            "( ci.requirement_description + ci.settlement_rules + ci.five_consistent + ci.maker_complaints ) AS businessRationalityPoints, " +
            "ci.compliance_opinion, " +
            "( ci.crowdsourcing_contract + ci.personnel_compliance + ci.issuing_rationality) AS compliancePoints, " +
            "ci.evidence_chain_opinion, " +
            "( ci.deliverables + ci.authenticity_of_deliverables + ci.acceptance_sheet + ci.personnel_punch_in ) AS evidenceChainPoints  " +
            "FROM " +
            "zdd_compliance_intelligence ci  " +
            "WHERE " +
            "ci.compid = #{compid} " +
            "AND ci.publish = 1 ")
    IntelligentComplianceDisplayDto intelligentComplianceDisplay(@Param("compid") Integer compid);

}
